﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.Windows;
using Dynamic_UI_Framework.Items.Regions;
using System.Collections.ObjectModel;
using Microsoft.Practices.ServiceLocation;
using System.Xml.Linq;
using System.IO;

namespace Dynamic_UI_Framework
{
    [Export(typeof(RegionController))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class RegionController
    {
        public RegionController()
        {
            Regions = new ObservableCollection<IRegion>();
            Paths = new Dictionary<string, XElement>();
            Load();
        }

        public ObservableCollection<IRegion> Regions { get; private set; }
        public Dictionary<string, XElement> Paths { get; private set; }

        private IRegion currentRegion;
        public IRegion CurrentRegion {
            get { return currentRegion; }
            set {
                if (currentRegion != null && currentRegion.Name.Equals(value.Name))
                    return;

                bool isEdit = currentRegion != null;
                if (isEdit)
                {
                    MessageBoxResult res = MessageBox.Show("是否对当前页面进行保存？", "Save", MessageBoxButton.YesNo);
                    if (res == MessageBoxResult.Yes)
                        Save();
                }

                currentRegion = value;
                RaiseCurrentRegionChanged();
            }
        }

        public void CreateRegion(IAdapter adapter)
        {
            string name = DialogHelper.DialogName(string.Empty);
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("string.IsNullOrEmpty(name)");
                return;
            }
            IVisual v = adapter.DefaultBuild(name);
            this.CurrentRegion = v as IRegion;
            this.Regions.Add(v as IRegion);
        }

        public void Save()
        {
            IBuilder builder = ServiceLocator.Current.GetInstance<IBuilder>("Region");
            if (builder != null)
            {
                UINode node = builder.GetUINode(this.currentRegion);
                XElement ele = node.WriteXml();
                if (Directory.Exists(UISetting.RegionPath) == false)
                    Directory.CreateDirectory(UISetting.RegionPath);
                ele.Save(UISetting.RegionPath + "\\" + this.currentRegion.Name + ".xml");

                if (Paths.ContainsKey(node.Name) == false)
                    Paths.Add(node.Name, ele);
            }
        }

        public void Load()
        {
            string[] files = Directory.GetFiles(UISetting.RegionPath, "*.xml", SearchOption.TopDirectoryOnly);
            foreach (var f in files)
            {
                XElement ele = XElement.Load(f);
                UINode node = new UINode();
                node.LoadXml(ele);
                IBuilder builder = ServiceLocator.Current.GetInstance<IBuilder>(node.TypeName);
                IVisual v = builder.Build(node);
                Regions.Add(v as IRegion);

                if (Paths.ContainsKey(node.Name) == false)
                    Paths.Add(node.Name, ele);
            }
        }

        private void RaiseCurrentRegionChanged()
        {
            var handle = CurrentRegionChanged;
            if (handle != null)
                handle(this, new EventArgs());
        }
        public event EventHandler CurrentRegionChanged;
    }
}
